/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.attachment.service.plugins  
 */
package com.apache.attachment.service.plugins;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.attachment.entity.AttachmentRegister;
import com.apache.attachment.entity.AttachmentStore;
import com.apache.attachment.manager.AttachmentRegisterManager;
import com.apache.attachment.manager.AttachmentStoreManager;
import com.apache.attachment.mommon.AttachmentStoreVo;
import com.apache.attachment.mommon.AttachmentUtil;
import com.apache.cache.util.Validator;
import com.apache.tools.StrUtil;

/**
 * description:  
 * @author mhguo 创建时间：2015-5-29  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
public class AttachmentPlugin implements PluginConnector {
	private Logger logger = LoggerFactory.getLogger(DelAttaPlugin.class);

	private AttachmentStoreManager attachmentStoreManager;

	private AttachmentRegisterManager attachmentRegisterManager;

	/**
	 *根据objType或者objId查询附件信息实体集合或者VO集合
	 */
	@Override
	public Object execute(ParamsVo param) throws Exception {
		this.logger.info("---------进入插件AttachmentPlugin----");
		String key = param.getMethodKey();
		ResultEntity result = new ResultEntity();
		result.setResult("false");

		String objType = String.valueOf(param.getParams("objType"));
		String objId = String.valueOf(param.getParams("objId"));
		System.out.println("--objType:" + objType);
		if (Validator.isNull(objType) || Validator.isNull(objId)) {
			result.setResult("false");
			result.setMessage("接口必要参数请正确填写");
			return result;
		}

		AttachmentRegister ar = getRegister(objType);
		if (Validator.isEmpty(ar)) {
			result.setResult("false");
			result.setMessage("实体未注册!");
			return result;
		}

		//权限检查
		String ip = StrUtil.doNull(ar.getIpAddress());

		if (StrUtil.isNotEmptyWithTrim(ip)) {
			boolean ipflag = checkIp(String.valueOf(param.getParams("remoteAddr")), ar.getIpAddress());
			if (!ipflag) {
				result.setResult("false");
				result.setMessage("无访问权限！");
				return result;
			}
		}

		ParamsVo vo = new ParamsVo();
		AttachmentStore as = new AttachmentStore();
		as.setObjId(objId);
		as.setObjType(objType);

		if ("attavolist".equals(key)) {//得到附件vo集合
			vo.setObj(as);
			List<AttachmentStore> asList = attachmentStoreManager.getList(vo);
			List<AttachmentStoreVo> asvoList = new ArrayList<AttachmentStoreVo>();
			AttachmentStoreVo asvo;
			if (!Validator.isEmpty(asList)) {
				for (int i = 0; i < asList.size(); i++) {
					asvo = copyAttaProp(as);
					asvoList.add(asvo);
				}
				result.setEntity(asvoList);
				result.setMessage("查询成功!");
				result.setResult("true");
			} else {
				result.setMessage("无附件信息!");
				result.setResult("false");
			}
		} else if ("attalist".equals(key)) {//得到附件实体集合
			vo.setObj(as);
			List<AttachmentStore> asList = attachmentStoreManager.getList(vo);
			if (!Validator.isEmpty(asList)) {
				result.setEntity(asList);
				result.setMessage("查询成功!");
				result.setResult("true");
			} else {
				result.setMessage("无附件信息!");
				result.setResult("false");
			}
		} else if ("getMainPic".equals(key) && "1".equals(ar.getAttaSort())) {//得到主图信息
			as.setMainPicFlag("1");
			vo.setObj(as);
			List<AttachmentStore> asList = attachmentStoreManager.getList(vo);
			if (!Validator.isEmpty(asList)) {
				AttachmentStore asmain = asList.get(0);
				result.setEntity(asmain);
				result.setMessage("查询成功!");
				result.setResult("true");
			} else {
				result.setMessage("无附件信息!");
				result.setResult("false");
			}
		} else if ("getMainPic".equals(key) && !"1".equals(ar.getAttaSort())) {
			result.setMessage("非图片库类型附件，没有主图信息！");
			result.setResult("false");
		}

		return result;
	}

	/**
	 *拷贝实体数据到VO
	 */
	private AttachmentStoreVo copyAttaProp(AttachmentStore atta) throws Exception {
		AttachmentStoreVo vo = new AttachmentStoreVo();
		vo.setAttaUrl(AttachmentUtil.getDefaultAurl(atta));
		vo.setAttaId(atta.getSid());
		vo.setAttaName(atta.getAttachmentName());
		vo.setAttaType(atta.getAttachmentType());
		vo.setAttaDesc(atta.getAttachmentDesc());
		return vo;
	}

	/**
	 * 访问ip验证
	 */
	private boolean checkIp(String userIp, String defineIp) {
		String uIp = StrUtil.doNull(userIp);
		String defIp = StrUtil.doNull(defineIp);
		if (StrUtil.isEmptyWithTrim(defIp)) {
			return true;
		}
		if (defIp.indexOf(uIp) != -1) {
			return true;
		}
		return false;
	}

	/**
	 * 通过objType得到实体注册信息
	 */
	public AttachmentRegister getRegister(String objType) {
		AttachmentRegister ar = new AttachmentRegister();
		ar.setRegEname(objType);
		ParamsVo vo = new ParamsVo();
		vo.setObj(ar);
		List<AttachmentRegister> arList = attachmentRegisterManager.getList(vo);
		if (arList.size() > 0) {
			ar = arList.get(0);
		}
		return ar;
	}

	public void setAttachmentStoreManager(AttachmentStoreManager attachmentStoreManager) {
		this.attachmentStoreManager = attachmentStoreManager;
	}

	public void setAttachmentRegisterManager(AttachmentRegisterManager attachmentRegisterManager) {
		this.attachmentRegisterManager = attachmentRegisterManager;
	}

}
